package com.knowif.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.knowif.dto.TutorInfoDto;
import com.knowif.pojo.TutorInfo;
import com.knowif.pojo.TutorInfoExample;
import com.knowif.search.SearchCondition;
import com.knowif.service.Tutor_service;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;

/**
 * @author xingyao
 * @Description: 导师功能控制台
 */
@RestController
@RequestMapping("/tutor")
public class TutorController {
	
	@Autowired
	private Tutor_service tutor_service;
	

    @PostMapping("/save")
    public RestBean<?> save(@RequestBody TutorInfoDto tutorInfoDto){
        try {
        	return tutor_service.saveTutor(tutorInfoDto);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/update")
    public RestBean<?> update(@RequestBody TutorInfoDto tutorInfoDto){
        try {
            return tutor_service.updateTutor(tutorInfoDto);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @DeleteMapping("/delete/{tUerId}")
    public RestBean<?> delete(@PathVariable("tUerId") int tUerId){
        try {
            return tutor_service.deleteTutor(tUerId);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition){
        try {
            RestBean<?> restBean = tutor_service.selectTutorInfo(searchCondition.getParameterMap());
            if(restBean.getCode() != 200){
                return restBean;
            }else{
                PageInfo<TutorInfo> result = (PageInfo<TutorInfo>)restBean.getresult();
                PageInfo<TutorInfoDto> pageInfo = new PageInfo<TutorInfoDto>(TutorInfoDto.tutorInfo2TutorInfoDto(result.getList()));
                pageInfo.setTotal(result.getTotal());
                return RestBean.build(RestBean.success_code,"",pageInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }


}
